package com.nerve.android.cloudoffice.service.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;

import com.alibaba.fastjson.JSON;
import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Department;
import com.nerve.android.cloudoffice.domain.User;
import com.nerve.android.cloudoffice.service.NetService;

/**
 * @project: Cloudoffice
 * @file: DepartmentData.java
 * @package: org.nerve.android.net
 * @description:
 *	组织管理的中间数据层
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: Aug 15, 2013
 * @change log:
 */
public final class DepartmentData {
	private static final String URL = Const.HOST + "department/";
	
	private static final String USER_URL = Const.HOST + "user/";
	
	private static List<Department> DepartmentList;
	
	@SuppressLint("UseSparseArrays")
	private static Map<Integer, List<User>> UserList = new HashMap<Integer, List<User>>();
	
	private static Map<String, String> buildDepartmentMap(Department d){
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", d.getName());
		return map;
	}
	
	public static Reply addDepartment(Department d){
		Reply re = NetService.post(URL + "add.htm", buildDepartmentMap(d));
		if(!re.isError()){
			int id = Integer.valueOf(re.getMessage().toString());
			d.setId(id);
			
			DepartmentList.add(d);
		}
		
		return re;
	}
	
	/**
	 * @method name: deleteDepartment
	 * @description: 删除指定id的部门
	 * @return type: Reply
	 *
	 * @param id
	 * @return
	 */
	public static Reply deleteDepartment(int id){
		Reply re = NetService.get(URL + "delete.htm?id=" + id);
		if(!re.isError()){
			for(int i=0;i<DepartmentList.size();i++){
				if(DepartmentList.get(i).getId() == id){
					DepartmentList.remove(i);
					break;
				}
			}
		}
		
		return re;
	}
	
	/**
	 * @method name: getDepartmentList
	 * @description: 
	 * @return type: List<Department>
	 *
	 * @return
	 */
	public static List<Department> getDepartmentList(){
		//如果为null 或者长度为0 ，先从服务器中获取数据
		if(DepartmentList == null || DepartmentList.size() == 0){
			Reply re = NetService.get(URL+"list.htm");
			
			System.out.println("reply=========="+re);
			if(!re.isError()){
				List<Department> list = JSON.parseArray(re.getMessage().toString(), Department.class);
				if(list != null){
					DepartmentList = list;
				}
			}else{
				if(DepartmentList == null)
					DepartmentList = new ArrayList<Department>();
			}
		}
		
		return DepartmentList;
	}
	
	public static Department getDepartment(int index){
		return getDepartmentList().get(index);
	}
	
	/**
	 * @method name: addUser
	 * @description: 增加一个新职员
	 * @return type: Reply
	 *
	 * @param did
	 * @param user
	 * @return
	 */
	public static Reply addUser(int did, User user){
		Map<String, String> map = new HashMap<String, String>();
		map.put("user.name", user.getName());
		map.put("user.password", user.getPassword());
		map.put("user.position", user.getPosition()+"");
		map.put("user.cellPhone", user.getCellPhone());
		map.put("user.sex", user.getSex() + "");
		map.put("user.address", user.getAddress());
		map.put("user.birthday", user.getBirthday());
		map.put("did", did+"");
		
		Reply re = NetService.post(USER_URL + "add.htm", map);
		if(!re.isError()){
			user.setId(Integer.valueOf(re.getMessage().toString()));
			UserList.get(did).add(user);
		}
		
		return re;
	}
	
	/**
	 * @method name: deleteUser
	 * @description: 删除一个用户 
	 * @return type: Reply
	 *
	 * @param did
	 * @param id
	 * @return
	 */
	public static Reply deleteUser(int did, int id){
		Reply re = NetService.get(USER_URL + "update.htm?updateType=2&id=" + id);
		if(!re.isError()){
			List<User> ul = UserList.get(did);
			for(int i=0;i<ul.size();i++){
				if(ul.get(i).getId() == id){
					ul.remove(i);
					break;
				}
			}
		}
		
		return re;
	}
	
	/**
	 * @method name: getUserList
	 * @description: 根据部门id获取对应的职员列表
	 * @return type: List<User>
	 *
	 * @param did
	 * @return
	 */
	public static List<User> getUserList(int did){
		List<User> ul = UserList.get(did);
		if(ul == null){
			Reply re = NetService.get(USER_URL + "list.htm?pageSize=100&did=" + did);
			
			if(!re.isError()){
				ul = JSON.parseArray(re.getMessage().toString(), User.class);
				
				if(ul == null)
					ul = new ArrayList<User>();
			}
			
			UserList.put(did, ul);
		}
		
		return ul;
	}
	
	/**
	 * @method name: updateUser
	 * @description: 
	 * @return type: Reply
	 *
	 * @param id
	 * @param type
	 * @param value
	 * @return
	 */
	public static Reply updateUser(int id, int type, String value){
		Map<String, String> pp = new HashMap<String, String>();
		pp.put("id", id+"");
		pp.put("updateType", type+"");
		pp.put("updateValue", value);
		
		Reply re = NetService.post(USER_URL + "update.htm", pp);
		
		return re;
	}
	
	public final static Map<String, Integer> MALE_HEADER = new HashMap<String, Integer>();
	public final static Map<String, Integer> FEMALE_HEADER = new HashMap<String, Integer>();
	static{
		MALE_HEADER.put("man_0", R.drawable.man_0);
		MALE_HEADER.put("man_1", R.drawable.man_1);
		MALE_HEADER.put("man_2", R.drawable.man_2);
		MALE_HEADER.put("man_3", R.drawable.man_3);
		MALE_HEADER.put("man_4", R.drawable.man_4);
		MALE_HEADER.put("man_5", R.drawable.man_5);
		MALE_HEADER.put("man_6", R.drawable.man_6);
		MALE_HEADER.put("man_7", R.drawable.man_7);
		MALE_HEADER.put("man_8", R.drawable.man_8);
		MALE_HEADER.put("man_9", R.drawable.man_9);
		MALE_HEADER.put("man_10", R.drawable.man_10);
		MALE_HEADER.put("man_11", R.drawable.man_11);
		
		FEMALE_HEADER.put("woman_0", R.drawable.woman_0);
		FEMALE_HEADER.put("woman_1", R.drawable.woman_1);
		FEMALE_HEADER.put("woman_2", R.drawable.woman_2);
		FEMALE_HEADER.put("woman_3", R.drawable.woman_3);
		FEMALE_HEADER.put("woman_4", R.drawable.woman_4);
		FEMALE_HEADER.put("woman_5", R.drawable.woman_5);
		FEMALE_HEADER.put("woman_6", R.drawable.woman_6);
		FEMALE_HEADER.put("woman_7", R.drawable.woman_7);
		FEMALE_HEADER.put("woman_8", R.drawable.woman_8);
		FEMALE_HEADER.put("woman_9", R.drawable.woman_9);
		FEMALE_HEADER.put("woman_10", R.drawable.woman_10);
		FEMALE_HEADER.put("woman_11", R.drawable.woman_11);
	}
	
	
	/**
	 * @method name: getHeaderResource
	 * @description: 获取头像资源id
	 * @return type: int
	 *
	 * @param sex
	 * @param header
	 * @return
	 */
	public static int getHeaderResource(int sex, String header){
		Integer rid = null; 
		if(sex == User.MALE)
			rid = MALE_HEADER.get(header);
		else
			rid = FEMALE_HEADER.get(header);
		
		if(rid == null)
			rid = R.drawable.ic_launcher;
		
		return rid;
	}
}